package com.ydy.controller;

import com.ydy.pojo.Carousel;
import com.ydy.pojo.Category;
import com.ydy.pojo.vo.CategoryVO;
import com.ydy.pojo.vo.NewItemsVO;
import com.ydy.service.CarouselService;
import com.ydy.service.CategoryService;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(value = "首页", tags = {"用于首页相关数据的接口"})
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 查询首页显示的轮播图
     * @Title: carousel
     * @author: dy.yin 2022/1/14 9:07
     * @param: []
     * @return: {@link ReturnResult }
     */
    @ApiOperation(value = "查询所有显示的首页轮播图",notes = "查询所有显示的首页轮播图",httpMethod = "GET")
    @GetMapping("/carousel")
    public ReturnResult carousel(){
        List<Carousel> list = carouselService.queryAll();
        return ReturnResult.ok(list);
    }

    /**
     * 查询所有的一级分类
     * @Title: cats
     * @author: dy.yin 2022/1/14 9:29
     * @return: {@link ReturnResult }
     */
    @ApiOperation(value = "查询所有一级分类",notes = "查询所有一级分类",httpMethod = "GET")
    @GetMapping("/cats")
    public ReturnResult cats(){
        List<Category> list = categoryService.queryAllRootLevelCategory();
        return ReturnResult.ok(list);
    }

    /**
     * 根据一级id查询子分类
     * @Title: subCat
     * @author: dy.yin 2022/1/14 9:32
     * @param: [rootCatId]   一级分类id
     * @return: {@link ReturnResult }
     */
    @ApiOperation(value = "根据一级id查询子分类",notes = "根据一级id查询子分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public ReturnResult subCat(@PathVariable String rootCatId){
        if(StringUtils.isEmpty(rootCatId)){
            ReturnResult.errorMsg("fuck you ");
        }
        List<CategoryVO> list = categoryService.querySubCategoryByRootId(rootCatId);
        return ReturnResult.ok(list);
    }


    /**
     * 根据一级分类查询最新6个产品
     * @Title: sixNewItems
     * @author: dy.yin 2022/1/14 9:51
     * @param: [rootCatId]  一级分类id
     * @return: {@link ReturnResult }
     */
    @ApiOperation(value = "根据一级id最新6个商品",notes = "根据一级id最新6个商品",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public ReturnResult sixNewItems(@PathVariable String rootCatId){
        if(StringUtils.isEmpty(rootCatId)){
            ReturnResult.errorMsg("fuck you ");
        }
        List<NewItemsVO> list = categoryService.querySixItemsByRootId(rootCatId);
        return ReturnResult.ok(list);
     }

}
